﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ConfApp.Infrastructure;
using ConfApp.Models;
using ConfApp.Services;
using Microsoft.Practices.Prism.Commands;

namespace ConfApp.ViewModels
{
    public class TrackListViewModel : ViewModel
    {
        private readonly IConfDataStoreLocator _confDataStoreLocator;

        private ObservableCollection<TrackInfoViewModel> _observableTrackInfo;
        private CollectionViewSource _trackInfoViewSource;

        public TrackListViewModel(
            IConfDataStoreLocator confDataStoreLocator,
            INavigationService navigationService)
            : base(navigationService)
        {
            _confDataStoreLocator = confDataStoreLocator;
            Initialize();
        }

        private void Initialize()
        {
            _version = -1;
            Refresh();

            this.TrackSelectedCommand = new DelegateCommand<TrackInfoViewModel>(
                t =>
                {
                    if(t==null) return;
                    NavigationService.Navigate(
                        new Uri("/Views/SessionListView.xaml?track=" + t.Code, UriKind.Relative));
                });

            this.IsBeingActivated();
        }

        private int _version;
        private void Refresh()
        {
            var version = _confDataStoreLocator.GetStore().Version;
            if (version > _version)
            {
                _version = version;
                _observableTrackInfo = new ObservableCollection<TrackInfoViewModel>();

                _trackInfoViewSource = new CollectionViewSource {Source = _observableTrackInfo};
                foreach (var track in _confDataStoreLocator.GetStore().GetTracks().OrderBy(t=>t.Code))
                {
                    _observableTrackInfo.Add(new TrackInfoViewModel(track,NavigationService));
                }
                //_observableSessionInfoGroups1 = new ObservableCollection<GroupList<SessionInfoViewModel>>();
                //var sessionInfoViewModelGroups1 =
                //    GroupSessions(_confDataStoreLocator.GetStore().GetSessionsByDayIndex(1));
                //sessionInfoViewModelGroups1.ForEach(_observableSessionInfoGroups1.Add);

                //_sessionInfoGroups1ViewSource = new CollectionViewSource
                //                                        {Source = _observableSessionInfoGroups1};
            }
        }

        //private List<GroupList<SessionInfoViewModel>> GroupSessions(IEnumerable<Session> sessions)
        //{
        //    var result = new List<GroupList<SessionInfoViewModel>>();
        //    var slots = sessions.OrderBy(s => s.Slot.Day.Date).ThenBy(s=>s.Slot.ToTimeSpanString()).Select(s => s.Slot.ToTimeSpanString()).Distinct();
        //    var sessionInfos = sessions.OrderBy(s => s.Slot.Day.Date).ThenBy(s => s.Slot.ToTimeSpanString()).Select(s => new SessionInfoViewModel(s, NavigationService));
        //    var groups = new Dictionary<string, GroupList<SessionInfoViewModel>>();
        //    foreach (var slot in slots)
        //    {
        //        var group = new GroupList<SessionInfoViewModel>(slot);
        //        result.Add(group);
        //        groups[slot] = group;
        //    }
        //    foreach (var sessionInfo in sessionInfos)
        //    {
        //        groups[sessionInfo.Slot.Trim()].Add(sessionInfo);
        //    }
        //    return result;
        //}

        public ICollectionView TrackInfos
        {
            get
            {
                Refresh();
                return _trackInfoViewSource.View;
            }
        }

        public DelegateCommand<TrackInfoViewModel> TrackSelectedCommand { get; set; }

        //public ICollectionView SessionInfos1
        //{
        //    get
        //    {
        //        Refresh();
        //        return _sessionInfoGroups1ViewSource.View;
        //    }
        //}

        public override void IsBeingActivated()
        {

        }
    }
}
